<?php
/**
 * (jobs) Rakuten产品变化时，更新关联基础数据
 */
namespace App\Jobs\Products;

use App\Models\Basics\AmazonCountry;
use App\Models\Basics\ExchangeRate;
use App\Models\Basics\SysICountry;
use App\Models\Basics\SysPlat;
use App\Models\Basics\Transport;
use App\Models\MonthlyReport\Cost\LogisticsBasics;
use App\Models\Operation\ProfitSku;
use App\Models\Product\ProductPacking;
use App\Models\Product\ProductRakutenSku;
use App\Models\Product\ProductRakutenSkuCCS;
use App\Models\Product\ProductSku;
use App\Models\SsBasic\AreaSku;
use App\Models\SsBasic\JpSku;
use App\Models\Stocking\StockingSetDate;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class RakutenSkuRelate implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $productRakutenSku;

    protected $userId;

    /**
     * RakutenSkuRelate constructor.
     * @param ProductRakutenSku $productRakutenSku
     * @param $userId
     */
    public function __construct(ProductRakutenSku $productRakutenSku,$userId)
    {
        $this->productRakutenSku = $productRakutenSku;
        $this->userId = $userId;
    }

    /**
     * Execute the job.
     * @return void
     */
    public function handle()
    {
        $this->exeFillData($this->productRakutenSku,$this->userId);
    }

    private function exeFillData($productRakutenSku,$userId){
        $this->_other($productRakutenSku,$userId);
    }

    private function _other(ProductRakutenSku $productRakutenSku,$userId){
        //更新产品表在售状态
        $sku = ProductSku::getModelById($productRakutenSku->psku_id);
        $sku->is_sales = 1;
        $sku->save();
        //更新销售
        $ccs = ProductRakutenSkuCCS::returnSellerId($productRakutenSku->country_id,$sku->classify_id,$userId);
        $productRakutenSku->seller_id = $ccs->seller_id ?? $userId;
        $productRakutenSku->save();

        //添加毛利率表
        $currencyCode = AmazonCountry::getCurrencyCodeByCountryId($productRakutenSku->country_id);
        $rateOdds = ExchangeRate::getRateOddsToRMB($currencyCode,time());
        $areaId = SysICountry::getAreaIdByCountryId($productRakutenSku->country_id);
        $transport = Transport::getTransport($areaId);
        $shipment = LogisticsBasics::getLastLogistics($areaId,$productRakutenSku->psku_id);
        if($shipment){
            $avgFee = true;
            $airFee = $seaFee = $shipment->price;
        }else{
            $avgFee = false;
            $airFee = $transport->air_fee ?? 0;
            $seaFee = $transport->sea_fee ?? 0;
        }
        $tplRMB = ProductSku::getPuPriceNoTax($sku);
        $pCycle = ProductSku::getPuCycle($sku);
        $shipWeight = ProductPacking::getShipWeight($productRakutenSku->psku_id);
        $vat = 0; //$vat = AmazonCountry::getVatByCountryId($productRakutenSku->country_id);
        ProfitSku::addProfitSkuRakuten($productRakutenSku,$rateOdds,$tplRMB,$shipWeight,$airFee,$seaFee,$vat,$avgFee);

        ////添加备货，提货基础数据(备货站点sku)
        //AreaSku::addAreaSku($areaId,$productRakutenSku->psku_id,$transport->air_day ?? 0,$transport->sea_day ?? 0,$pCycle,$tplRMB);

        //添加(JP平台)备货，提货基础数据(JP平台备货sku)
        JpSku::addJpSku(SysPlat::PLAT_RAKUTEN,$productRakutenSku->psku_id,$transport->air_day ?? 0,$transport->sea_day ?? 0,$pCycle,$tplRMB);

        //添加备货SetDate
        $countryIds = SysICountry::getCountryIdsByAreaId($areaId);
        $pCycleForSetDate = StockingSetDate::getPCycle($countryIds,$sku->classify_id);
        StockingSetDate::addSetDate($areaId,$sku->classify_id,$pCycleForSetDate,$transport->sea_day ?? 0);
    }
}
